#include "CameraController.h"
#include "World.h"
#include "utility.h"

void CameraController::Initialization()
{
	mCamera = mSceneMgr->getCamera("PlayerCam");
	pPlayerController = static_cast<PlayerController*>(mWorld->GetController("Player"));
	Vector3 pos = pPlayerController->getPlayerModel()->getPostion();
	mCameraDistance = 50;
	Vector3 delta(0, 1, -4);
	delta.normalise();
	mCamera->setPosition(pos + mCameraDistance*delta);
	mCamera->lookAt(pos);
	mRotateAxis = Vector3::UNIT_Y;
	SetCameraStatus(SURROUND_PLAYER);
	//SetCameraStatus(STOP);
	//SetCameraStatus(FOLLOW_PLAYER);
}

void CameraController::AddTime( Real deltatime )
{
	Vector3 pos = pPlayerController->getPlayerModel()->getPostion();
	if (mStatus == STOP)
	{
		//do nothing
	}
	else if (mStatus == SCAN_WORLD)
	{

	}
	else if (mStatus == FOLLOW_PLAYER)
	{
		Vector3 delta(0, 1, -4);
		delta.normalise();
		mCamera->setPosition(pos + mCameraDistance*delta);
		mCamera->lookAt(pos);
	}
	else if (mStatus == SURROUND_PLAYER)
	{
		Radian rotatedAngle;
		if ((mAngleLeft-mAngleSpeed*deltatime).valueRadians()<0)
		{
			rotatedAngle=mAngleLeft;
		}
		else rotatedAngle=mAngleSpeed*deltatime;
		mAngleLeft -= rotatedAngle;
		Quaternion t(rotatedAngle, mRotateAxis);
		Vector3 delta = mCamera->getPosition() - pos;
		delta.normalise();
		delta = t*delta;
		mCamera->setPosition(pos + mCameraDistance*delta);
		mCamera->lookAt(pos);
		if (equal(mAngleLeft.valueRadians(),0)) SetCameraStatus(STOP);
	}
}

CameraController::CameraController( SceneManager *sm ):
mSceneMgr(sm)
{
}

void CameraController::Send( PapaBallMsg msg )
{
	throw std::exception("The method or operation is not implemented.");
}

void CameraController::SetCameraStatus( CAMERA_STATUS s )
{
	mStatus = s;
	if (mStatus == SURROUND_PLAYER)
	{
		mAngleLeft = Radian(Math::PI*2);
		mAngleSpeed = mAngleLeft / 10;
	}
}

CameraController::CAMERA_STATUS CameraController::GetCameraStatus()
{
	return mStatus;
}

void CameraController::SetCameraDistance( Real x )
{
	mCameraDistance = x;
}

Ogre::Real CameraController::GetCameraDistance()
{
	return mCameraDistance;
}


